const { makeInvoker } = require('awilix-express')
const { Router } = require('express')
const { responseHelper } = require('../helper')

const api = makeInvoker(
  ({
    getRolePage,
    getRoleDetail,
    updateRole,
    deleteRole,
    roleAuthorization,
    createRole,
  }) => {
    return {
      getRolePage: responseHelper(async (req, res) => {
        return await getRolePage.execute(req.query)
      }),
      getRoleDetail: responseHelper(async (req, res) => {
        return await getRoleDetail.execute(req.params.id)
      }),
      deleteRole: responseHelper(async (req, res) => {
        return await deleteRole.execute(req.params.id)
      }),
      createRole: responseHelper(async (req, res) => {
        return await createRole.execute(req.body)
      }),
      updateRole: responseHelper(async (req, res) => {
        return await updateRole.execute({
          id: req.params.id,
          ...req.body,
        })
      }),
      roleAuthorization: responseHelper(async (req, res) => {
        return await roleAuthorization.execute(req.params.id, req.body)
      }),
    }
  }
)

module.exports = ({ all }) => {
  const router = Router()
  router
    .get('/', all, api('getRolePage'))
    .post('/', all, api('createRole'))
    .get('/:id', all, api('getRoleDetail'))
    .put('/:id', all, api('updateRole'))
    .post('/:id/authorization', all, api('roleAuthorization'))
    .delete('/:id', all, api('deleteRole'))
  return router
}
